import logging
import os

from config import logs_path


class DoLog():
    def getLogger(self, name):
        # 实例化记录器
        logger = logging.getLogger(name)

        # 设置写的平台
        f = os.path.join(logs_path, 'myLog.txt')
        fh = logging.FileHandler(f, encoding="utf-8")

        # 设置书写格式
        a = "%(asctime)s  %(filename)s  %(lineno)d  %(levelname)s:%(message)s"
        a2 = logging.Formatter(a)

        # 设置书写等级
        logger.setLevel(logging.INFO)

        # 将平台与格式关联
        fh.setFormatter(a2)

        # 将平台与记录器关联
        logger.addHandler(fh)

        # 返回记录器
        return logger


if __name__ == '__main__':
    lg = DoLog().getLogger(__name__)

    lg.log(logging.INFO, "我是info级别的信息")
    lg.log(logging.DEBUG, "我是DEBUG级别的信息")
    lg.log(logging.ERROR, "我是ERROR级别的信息")
    lg.log(logging.INFO, "我是info级别的信息")
    lg.log(logging.INFO, "我是info级别的信息")
